package com.atguigu.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.constant.RedisConstant;
import com.atguigu.dao.SetmealDao;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.pojo.Setmeal;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Resource //按照IOC容器中bean对象的名字(id值)
            // @Autowired //默认类型
            //<bean id="setmealDao" class>
            // @Qualifier(value = "setmealDao")
    SetmealDao setmealDao;

    @Autowired
    JedisPool jedisPool;

    @Override
    public void add(Setmeal setmeal, Integer[] travelgroupIds) {
        //1.添加套餐游
        setmealDao.add(setmeal);
        //2.添加套餐游和跟团游的关系
        insert_Setmeal_Group(setmeal.getId() ,travelgroupIds);
        //3.把保存到数据库的图片名字保存到redis
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page= setmealDao.findPageByQueryString(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    public void  insert_Setmeal_Group (Integer setmealId,Integer[] travelgroupIds){
        for ( Integer travelgroupId : travelgroupIds) {
            Map map = new HashMap();
            map.put("setmealId",setmealId);
            map.put("groupId",travelgroupId);
            setmealDao.add_Setmeal_Group(map);
        }
    }



    /*移动端方法*/

    @Override
    public List<Setmeal> getSetmaeal() {
        return setmealDao.getSetmeal();
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }


}
